package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.enumeration.TeachingInterruptionReason;
import vn.edu.saigontech.ncms.model.teaching.TeachingInterruption;
import vn.edu.saigontech.ncms.service.TeachingInterruptionService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;

@SuppressWarnings("unchecked")
public class TeachingInterruptionServiceImpl extends
		HibernateGenericService<TeachingInterruption, Long> implements
		TeachingInterruptionService {

	@Override
	public Collection<TeachingInterruption> findBySubstituteTeacher(
			Long substituteTeacherId) {
		Criteria criteria = this.createCriteria();
		criteria.createCriteria("substituteTeacher").add(
				Restrictions.eq("id", substituteTeacherId));
		return criteria.list();
	}
	
	@Override
	public Collection<TeachingInterruption> findByMainTeacher(Long mainTeacherId) {
		Criteria criteria = this.createCriteria();
		criteria.createCriteria("teacher").add(
				Restrictions.eq("id", mainTeacherId));
		return criteria.list();
	}

	@Override
	public TeachingInterruption find(Long offeringId, Long teacherId, Date date) {
		Criteria criteria = this.createCriteria();
		criteria.createCriteria("offering").add(
				Restrictions.eq("id", offeringId));
		criteria.createCriteria("teacher")
				.add(Restrictions.eq("id", teacherId));
		criteria.add(Restrictions.eq("fromDate", date));
		return (TeachingInterruption) criteria.uniqueResult();
	}

	@Override
	public Collection<TeachingInterruption> findByDate(Date from, Date to) {
		Collection<TeachingInterruption> all = this.findAll();
		Collection<Date> range = DateUtil.sequence(from, to);
		Collection<TeachingInterruption> result = new LinkedHashSet<TeachingInterruption>();
		for (TeachingInterruption each : all) {
			Collection<Date> dates = DateUtil.sequence(each.getFromDate(), each
					.getToDate());
			dates.retainAll(range);
			if (!dates.isEmpty()) {
				result.add(each);
			}
		}
		return result;
	}

	@Override
	public Collection<TeachingInterruption> findMakeupByDate(Date from, Date to) {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("reason", TeachingInterruptionReason.CANCEL));
		Collection<TeachingInterruption> all = criteria.list();
		Collection<Date> range = DateUtil.sequence(from, to);
		Collection<TeachingInterruption> result = new LinkedHashSet<TeachingInterruption>();
		for (TeachingInterruption each : all) {
			if(Checker.isNotNull(each.getMakeupFromDate()) && Checker.isNotNull(each.getMakeupFromDate())) {
				Collection<Date> dates = DateUtil.sequence(each.getMakeupFromDate(), each
						.getMakeupToDate());
				dates.retainAll(range);
				if (!dates.isEmpty()) {
					result.add(each);
				}
			}
		}
		return result;
	}
}